You are training a transformer-based policy to imitate an expert by predicting the next discrete latent action for each timestep of an observation sequence. Given an input sequence of observation embeddings, the policy runs a single causal transformer block followed by a linear head that produces logits over a fixed latent action codebook. Training uses behavioral cloning: the mean cross-entropy between the predicted latent action distribution at each timestep and the ground-truth expert latent action at that timestep.
Implement the full forward pass and loss computation. The transformer block has the following structure (single head, pre-computed linear projections):
The loss is the average token-level cross-entropy between the logit distribution at each timestep and the corresponding target latent action index. There is no layer normalization in this block.
Your function receives the observation sequence obs of shape (T, d_model), the target latent action indices targets of shape (T,), and a dictionary weights containing the arrays W_q, W_k, W_v, W_o (each of shape (d_model, d_model)), W1 of shape (d_model, d_ff) with bias b1 of shape (d_ff,), W2 of shape (d_ff, d_model) with bias b2 of shape (d_model,), and the policy head W_head of shape (d_model, K) with bias b_head of shape (K,). Return the scalar behavioral cloning loss as a float.