ML-Agent cannot use model trained with GPU (ComputeShader)

Hello everyone.

I installed Unity ML-Agents following the official instructions. Created a test project from the “Making a New Learning Environment” page. Initially trained the model using CPU (Burst), and the model trained successfully and ran in Unity. Later, I tried training the model with GPU (ComputeShader). The model trained successfully but failed to run in Unity, throwing the following error:

InvalidOperationException: Tensor data cannot be read from, use .ReadbackAndClone() to allow reading from tensor.

Console logs / stack traces:

InvalidOperationException: Tensor data cannot be read from, use .ReadbackAndClone() to allow writting to the tensor.
Unity.Sentis.Tensor.SetItem[T] (System.Int32 d0, T value) (at ./Library/PackageCache/com.unity.sentis@c51693b52067/Runtime/Core/Tensor.cs:269)
Unity.Sentis.Tensor`1[T].set_Item (System.Int32 d0, T value) (at ./Library/PackageCache/com.unity.sentis@c51693b52067/Runtime/Core/TensorGeneric.cs:259)
Unity.Sentis.Tensor`1[T].set_Item (System.Int32 d1, System.Int32 d0, T value) (at ./Library/PackageCache/com.unity.sentis@c51693b52067/Runtime/Core/TensorGeneric.cs:249)
Unity.MLAgents.Inference.ActionMaskInputGenerator.Generate (Unity.MLAgents.Inference.TensorProxy tensorProxy, System.Int32 batchSize, System.Collections.Generic.IList`1[T] infos) (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Inference/GeneratorImpl.cs:193)
Unity.MLAgents.Inference.TensorGenerator.GenerateTensors (System.Collections.Generic.IReadOnlyList`1[T] tensors, System.Int32 currentBatchSize, System.Collections.Generic.IList`1[T] infos) (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Inference/TensorGenerator.cs:175)
Unity.MLAgents.Inference.ModelRunner.DecideBatch () (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Inference/ModelRunner.cs:220)
Unity.MLAgents.Policies.SentisPolicy.DecideAction () (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Policies/SentisPolicy.cs:134)
Unity.MLAgents.Agent.DecideAction () (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Agent.cs:1411)
Unity.MLAgents.Academy.EnvironmentStep () (at ./Library/PackageCache/com.unity.ml-agents@2b4ded88494d/Runtime/Academy.cs:590)
Unity.MLAgents.AcademyFixedUpdateStepper.Fixed 

Environment:

  • Unity: Unity 6 (6000.0.38f1) LTS
  • OS: Windows 10
  • ML-Agents: 3.0.0
  • Sentis: 2.1.2
  • Python: 3.10.12
  • Torch: 2.2.1+cu121
  • MLAgents pip: 1.1.0
  • MLAgents-envs pip: 1.1.0

Thank you for finding this, we will take a look.

Try changing the Inference Device. I had the same issue with a model trained on gpu and inference on gpu, but if I change the inference device to default the model ran without issues.

1 Like