M
Modular4mo ago
null

How to initialize a tensor[DType.int8] with random values of either: -1, 0, or 1?

Probably a dumb question, but how can I initialize a Tensor like so with random values of either -1, 0, or 1?
var a = Tensor[DType.int8](size)
var a = Tensor[DType.int8](size)
The rand functions I could find either don't let me specify the range, or work for other data types. Furthermore, is this really only for SIMD compatible sizes? Like what if I wanted it to have 10,000 elements? Is Tensor the wrong data type for this? I want to perform element-wise vector operations on it later, like xor(vector_a, vector_b) - but in a highly parallel way
9 Replies
benny
benny4mo ago
are you trying to work with vectors or tensors? if you don’t need n dimensions then a much better option is Buffer or even DTypePointer, what exactly are you trying to do?
null
null4mo ago
@benny Playing with hyperdimensional vectors, requires vectors with dimensions over 10,000 to be useful (this is a rule in Vector Symbolic Architecture). For example: https://torchhd.readthedocs.io/en/stable/generated/torchhd.random.html#torchhd.random Except I want to experiment with 3 valued systems rather than two. But anyway, to create a VSA you need operations like element wise multiplication, addition, and other operations (element wise XOR, etc, for the BSC architecture)
benny
benny4mo ago
ahhhh I understand
null
null4mo ago
Point being, I need a way to randomly initialize really large vectors (could be a million dimensions for example)
benny
benny4mo ago
very cool let me check some stuff randint is what you are looking for
benny
benny4mo ago
get the underlying data pointer from the tensor and give that as an argument to randint, ranged -1, 1
null
null4mo ago
Like this?
var a = Tensor[DType.int8](100)
randint[DType.int8](a._take_data_ptr(), 1, -1, 1)
var a = Tensor[DType.int8](100)
randint[DType.int8](a._take_data_ptr(), 1, -1, 1)
err, not 1.. Actually, maybe that's right? It explodes when I try and print a though. "number of elements to fill", I assume that is 100. so: randintDType.int8, 100, -1, 1) then when I try and print:
[3914:3914:20240711,145433.691824:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[3914:3914:20240711,145433.691868:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo test.mojo
#0 0x0000556167ca6438 (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1438)
#1 0x0000556167ca425e (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x129f25e)
#2 0x0000556167ca6acd (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1acd)
#3 0x00007f04855b9320 (/lib/x86_64-linux-gnu/libc.so.6+0x45320)
#4 0x00007f040000b85e
mojo crashed!
Please file a bug report.
[1] 3912 segmentation fault mojo test.mojo
[3914:3914:20240711,145433.691824:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[3914:3914:20240711,145433.691868:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo test.mojo
#0 0x0000556167ca6438 (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1438)
#1 0x0000556167ca425e (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x129f25e)
#2 0x0000556167ca6acd (/home/pat/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1acd)
#3 0x00007f04855b9320 (/lib/x86_64-linux-gnu/libc.so.6+0x45320)
#4 0x00007f040000b85e
mojo crashed!
Please file a bug report.
[1] 3912 segmentation fault mojo test.mojo
Ah, got it. a.unsafe_ptr() not a._take_data_ptr(). My intellisense betrayed me!
benny
benny4mo ago
yep, glad you got it working :)
Want results from more Discord servers?
Add your server