Repository: google-research/text-to-text-transfer-transformer

Issue:
unable to run model.train() on HuggingFace model
I have been finetuning hf model on several tasks for days, but since yesterday there have been errors when I run .train(), no matter on the tasks I created myself or already existing tasks. For example, on this code:

model.train(
    mixture_or_task_name="glue_cola_v002",
    steps=2000,
    save_steps=200,
    sequence_length={"inputs": 128, "targets": 16},
    split="train",
    batch_size=32,
    optimizer=functools.partial(transformers.AdamW, lr=1e-4),
)


The error report is like this:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-df772f2e2359> in <module>()
      6     split="train",
      7     batch_size=32,
----> 8     optimizer=functools.partial(transformers.AdamW, lr=1e-4),
      9 )

/usr/local/lib/python3.6/dist-packages/t5/models/hf_model.py in train(self, mixture_or_task_name, steps, save_steps, sequence_length, split, batch_size, optimizer, learning_rate_scheduler)
    326           attention_mask=self.to_tensor(batch["inputs_mask"]),
    327           decoder_attention_mask=self.to_tensor(batch["targets_mask"]),
--> 328           lm_labels=self.to_tensor(batch["targets"]),
    329       )
    330       loss.backward()

ValueError: too many values to unpack (expected 3)

Anyone having the same problem?


Discussion:
I set the optimizer in operative_config.gin,
using like:
```
run.optimizer = @optimize.AdafactorOptimizer
AdafactorOptimizer.beta1 = 0.0
AdafactorOptimizer.clipping_threshold = 1.0
AdafactorOptimizer.decay_rate = None
AdafactorOptimizer.epsilon1 = 1e-30
AdafactorOptimizer.epsilon2 = 0.001
AdafactorOptimizer.factored = True
AdafactorOptimizer.min_dim_size_to_factor = 128
AdafactorOptimizer.multiply_by_parameter_scale = True
```
Hope it helps.
> AdafactorOptimizer

Hello, I don't quite understand how this block of code helps with the issue? Does it mean I need to use this optimizer you defined? and how to call this optimizer?
> > AdafactorOptimizer
> 
> Hello, I don't quite understand how this block of code helps with the issue? Does it mean I need to use this optimizer you defined? and how to call this optimizer?

Usually you can do this. For example, to load the `gin` file, 
```
import gin
with gin.unlock_config():
  gin.parse_config_file("gs://t5-data/pretrained_models/base/operative_config.gin")
```

You have to do this, just before you call `model.train()` or `model.finetune()`. In your case, you can add your desired changes and then save it and execute this block to have those changes before you train or finetune. 


> > > AdafactorOptimizer
> > 
> > 
> > Hello, I don't quite understand how this block of code helps with the issue? Does it mean I need to use this optimizer you defined? and how to call this optimizer?
> 
> Usually you can do this. For example, to load the `gin` file,
> 
> ```
> import gin
> with gin.unlock_config():
>   gin.parse_config_file("gs://t5-data/pretrained_models/base/operative_config.gin")
> ```
> 
> You have to do this, just before you call `model.train()` or `model.finetune()`. In your case, you can add your desired changes and then save it and execute this block to have those changes before you train or finetune.

Hello, thank you for the explanation! But is this path already existed or I need to create it? When I run this block, there is error report saying: 
OSError: Unable to open file: gs://t5-data/pretrained_models/base/operative_config.gin. Searched config paths: [''].
@Sara-X The path in the example is of google storage location. (Yes it already exists).  In case you have `gin` file in your local machine, give the path to that local `gin` file residing on your machine. That means you have to have your own gin file where you will make these changes as mentioned above. (Here is an example `gin` file - [link](https://console.cloud.google.com/storage/browser/t5-data/experiments/objectives/obj-prefix_lm?prefix=operative_config.gin) )
> @Sara-X The path in the example is of google storage location. (Yes it already exists). In case you have `gin` file in your local machine, give the path to that local `gin` file residing on your machine. That means you have to have your own gin file where you will make these changes as mentioned above. (Here is an example `gin` file - [link](https://console.cloud.google.com/storage/browser/t5-data/experiments/objectives/obj-prefix_lm?prefix=operative_config.gin) )

Actually I am running it on google colab, so I should be able to import from "gs://t5-data/pretrained_models/base/operative_config.gin", right? Since I do not want to make changes, I just copied your block of code to colab, but somehow there is error
I guess the error is coming from the authentication issue. Just like the section "Train on TPU" section ([link](https://github.com/google-research/text-to-text-transfer-transformer/blob/master/notebooks/t5-trivia.ipynb)) I have a block in my code which sets up the GCS access. I ran into the same error when I didn't run this block. After I have set up access, it was working fine. (I am using the TPU runtime).

Resolving patch:
diff --git a/t5/models/hf_model.py b/t5/models/hf_model.py
--- a/t5/models/hf_model.py
+++ b/t5/models/hf_model.py
@@ -321,12 +321,13 @@ def train(
         self.save_checkpoint(self._step)
 
       self._model.zero_grad()
-      loss, _, _ = self._model(
+      outputs = self._model(
           input_ids=self.to_tensor(batch["inputs"]),
           attention_mask=self.to_tensor(batch["inputs_mask"]),
           decoder_attention_mask=self.to_tensor(batch["targets_mask"]),
           lm_labels=self.to_tensor(batch["targets"]),
       )
+      loss = outputs[0]
       loss.backward()
       optimizer.step()
       if learning_rate_scheduler:


Test patch: