{"record":{"id":"1506203a069a0151","repo":"dotnet/machinelearning","slug":"type-typeof-t-not-supported-in-data-loading","errorCode":null,"errorMessage":"Type {typeof(T)} not supported in data loading.","messagePattern":"Type (.+?) not supported in data loading\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.TorchSharp/Utils/FileUtils.cs","lineNumber":50,"sourceCode":"        /// Load a continuous segment of bytes from stream and parse them into a number array.\n        /// NOTE: this function is only for little-endian storage!\n        /// </summary>\n        /// <typeparam name=\"T\">should be a numeric type</typeparam>\n        /// <param name=\"stream\">the stream to read from its current position</param>\n        /// <param name=\"numElements\">expected number of parsed numbers</param>\n        /// <param name=\"tSize\">number of bytes occupied by the specified type</param>\n        /// <exception cref=\"NotSupportedException\">When the generic type T is not a valid numeric type.</exception>\n        /// <exception cref=\"ArgumentException\"/>\n        /// <exception cref=\"InvalidDataException\">When the contents in the stream don't match the need.</exception>\n        public static IEnumerable<T> LoadNumberArrayFromStream<T>(Stream stream, int numElements, int tSize)\n        {\n            if (stream == null || !stream.CanRead)\n            {\n                throw new ArgumentException($\"Stream should be non-null and its stream.CanRead property should be true.\");\n            }\n            if (!_validTypes.Contains(typeof(T)))\n            {\n                throw new NotSupportedException($\"Type {typeof(T)} not supported in data loading.\");\n            }\n\n            var numBytesConsumed = numElements * tSize;\n            var byteBuffer = new byte[numBytesConsumed];\n            var numBytesRead = stream.Read(byteBuffer, 0, numBytesConsumed);\n            if (numBytesConsumed != numBytesRead)\n            {\n                throw new InvalidDataException(\n                    $\"The number of bytes read from stream is less than expected. Please check the data files.\");\n            }\n\n            var targetBuffer = new T[numBytesConsumed / tSize];\n            Buffer.BlockCopy(byteBuffer, 0, targetBuffer, 0, numBytesConsumed);\n            return targetBuffer;\n        }\n    }\n}\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.TorchSharp/Utils/FileUtils.cs#L32-L68","documentation":"LoadNumberArrayFromStream restricts its generic type parameter T to a fixed set of numeric types (_validTypes) because it uses Buffer.BlockCopy over raw bytes; it throws NotSupportedException when T is any other type. Unsupported types cannot be safely blitted from the byte buffer.","triggerScenarios":"Calling LoadNumberArrayFromStream<T> with T outside the supported numeric set — e.g., LoadNumberArrayFromStream<decimal>, LoadNumberArrayFromStream<string>, LoadNumberArrayFromStream<double> if double is not in _validTypes — while loading tensor/model data.","commonSituations":"Trying to deserialize a checkpoint stored in one precision (float) into another type parameter, or generically calling with an unconstrained T inferred from a generic helper.","solutions":["Call with a supported numeric type — typically float or int (the types used by the model weights): LoadNumberArrayFromStream<float>(stream, n, sizeof(float)).","Match tSize to the chosen type's byte width (4 for float/int, 8 for long/double where supported).","Check _validTypes / the documented type list before choosing T; convert data after loading a supported type if needed.","Wrap in try-catch on NotSupportedException to report the unsupported type argument."],"exampleFix":"// before\nvar data = FileUtils.LoadNumberArrayFromStream<decimal>(stream, n, 16); // throws\n\n// after\nvar data = FileUtils.LoadNumberArrayFromStream<float>(stream, n, 4); // supported","handlingStrategy":"validation","validationCode":"var supported = new[] { typeof(float), typeof(int) /* + other _validTypes */ };\nif (!supported.Contains(typeof(T)))\n    throw new NotSupportedException($\"Type {typeof(T)} not supported for stream loading.\");","typeGuard":null,"tryCatchPattern":"try { var data = FileUtils.LoadNumberArrayFromStream<T>(stream, n, tSize); }\ncatch (NotSupportedException ex) { log.LogError(ex, \"Unsupported element type {Type}\", typeof(T)); throw; }","preventionTips":["Use float (with tSize=4) for standard model weights.","Don't call the generic loader with unconstrained type parameters.","Convert to other types only after loading a supported type."],"tags":["csharp","generics","unsupported-type","io"],"backgroundTag":"unsupported-dtype","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}