tinygrad: Metal gives Error Domain=MTLLibraryErrorDomain Code=1 "MTLLibrary is not formatted as a MetalLib file"
Hi, I am going through the tinygrad documentation and trying to run the basic Tensor example on abstractions.py
Environment:
- M1 Mac, Sonoma 14.0,
- Python 3.10.13
- tinygrad source install Nov 6th
try_tensor.py:
from tinygrad.tensor import Tensor
a = Tensor([2])
b = Tensor([3])
result = a + b
print(f"{a.numpy()} + {b.numpy()} = {result.numpy()}")
However I am getting an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tinygrad/tinygrad/tensor.py", line 126, in numpy
return self.detach().cast(dtypes.from_np(self.dtype.np)).contiguous().to('CPU').realize().lazydata.realized.toCPU().reshape(self.shape)
File "/tinygrad/tinygrad/tensor.py", line 105, in realize
run_schedule(self.lazydata.schedule())
File "/tinygrad/tinygrad/realize.py", line 27, in run_schedule
si.out.realized = Device[si.out.device].exec_ast(si.ast, output=si.out, inputs=si.inputs, var_vals=si.var_vals, **si.out._device_extra_args())
File "/tinygrad/tinygrad/ops.py", line 292, in exec_ast
if ast not in self.method_cache: self.method_cache[ast] = get_program()
File "/tinygrad/tinygrad/ops.py", line 289, in get_program
return self.to_program(k)
File "/tinygrad/tinygrad/ops.py", line 239, in to_program
display_name=k.display_name, runtime_args=runtime_args).build(self.compiler, self.runtime)
File "/tinygrad/tinygrad/ops.py", line 193, in build
self.lib = compiler.__wrapped__(self.prg) if getenv("DISABLE_COMPILER_CACHE") else compiler(self.prg)
File "/tinygrad/tinygrad/helpers.py", line 206, in wrapper
return diskcache_put(table, key, func(*args, **kwargs))
File "/tinygrad/tinygrad/runtime/ops_metal.py", line 51, in compile_metal
unwrap(library.serializeToURL_error_(Cocoa.NSURL.URLWithString_(f"file://{output_file.name}"), None))
File "/tinygrad/tinygrad/runtime/ops_metal.py", line 38, in unwrap
assert err is None, str(err)
AssertionError: Error Domain=MTLLibraryErrorDomain Code=1 "MTLLibrary is not formatted as a MetalLib file." UserInfo={NSLocalizedDescription=MTLLibrary is not formatted as a MetalLib file.}
This was the Metal program generated:
#include <metal_stdlib>
using namespace metal;
kernel void E_(device float* data0,
const device float* data1,
const device float* data2,
uint3 gid [[threadgroup_position_in_grid]],
uint3 lid [[thread_position_in_threadgroup]]) {
float val0 = *(data1+0);
float val1 = *(data2+0);
*(data0+0) = (val0+val1);
}
Compile options:
<MTLCompileOptionsInternal: 0x10b7eea90>
preprocessorMacros:
fastMathEnabled = 1
framebufferReadEnabled = 0
preserveInvariance = 0
optimizationLevel = MTLLibraryOptimizationLevelDefault
libraryType = MTLLibraryTypeExecutable
installName = <null>
compileSymbolVisibility = 0
allowReferencingUndefinedSymbols = 0
maxTotalThreadsPerThreadgroup = 0
languageVersion = default
About this issue
- Original URL
- State: open
- Created 8 months ago
- Reactions: 7
- Comments: 46 (10 by maintainers)
For all facing this issue -> just run the existing master with these two env variables “METAL_XCODE=1 DISABLE_COMPILER_CACHE=1”. It passes all the tests for m1 macos sonoma 14.x.x
Essentialy there is some issue with when you use the cached metal library in M1 air macos14.x.x atleast. With DISABLE_COMPILER_CACHE=1 in line 254 in device.py we call the wrapped attr… so basically without the disckcache added functionality and it re-compiles everytime which makes it work.
Got the hint from #2372 and the latest discussion there. this way u dont even need to do any change in the code.
#2372 resolves this issue for me!
I have the same error on macOS 13.4.1 trying to run the mnist example.
@dattienle2573 from tinygrad import Device Device.DEFAULT = “GPU”
@alexbaden can you try this: https://github.com/tinygrad/tinygrad/pull/2372 (still WIP)
Maybe I wasn’t clear enough, all conda python versions I’ve tried had made this bug occur.
Got this error on my conda python 3.12. So can’t say its working on 3.12 entirely.
@Leikoe great job investigating this!
Piggybacking from @SamRaymond 's work.
I got to this issue after trying to run the hello word matmul example in the README
with
GPU=1On Sonoma 14.2.1, MacBook Air M1 2020
This took a while… My M2 Air works like a charm, Metal was in the site-packages, for my M1 Pro 16, Metal wasn’t in the packages If I run METAL_XCODE=1 python docs/abstractions2.py with DEVICE = “GPU” this works for me, same for other files.
Running on M1 Macbook Pro 16 inch 2021 (14.2.1 (23C71))
MTLDynamicLibrary isn’t a viable solution, it isn’t supported on all devices. Macs with Intel or AMD gpus will suffer.
I am still having this problem - M1 MacBook Air (2020) with macOS 14.1.1. Interestingly with #2369 I get a different problem:
where line 61 is the call to
newLibraryWithData. It would seem something is whacky with exporting the compiled shader library on certain Mseries Macs? This worked fine on the exact same machine w/ macOS 13.x.I have macOS 13. Is 14 broken?