swig.
swig.
MModular
Created by swig. on 4/11/2024 in #questions
Segmentation Error Occurring With Set Implementation
Hi there, I'm having a very difficult time implementing a custom datatype into the builtin set object. For somebackground I am making a an AI model from scratch to play snake and am trying to create a training environment for n number of snakes in a generation. Each update of a generation moves the snake one pixel. I have designed the snake's move operation to add a xy coordinate to a set and remove the oldest xy coordinate from the set (this is determined by a deque implementation which is unrelated). Each portion of the snakes body is stored in a Position object which contains the x and y position and a simd vector containing the underlying data. The data structure is Hashable but when adding a position object, sometimes this works correctly but in some very small instances I get a segmentation error. Here is the code for the Position data structure I made. Let me know if there is a way to hash a tuple object or any other simpler way to hash xy positions?
alias dtype = DType.int16

@value
struct Position(KeyElement):
var x: SIMD[dtype, 1]
var y: SIMD[dtype, 1]
var data: SIMD[dtype, 2]
var hash: Int

fn __init__(inout self, x: SIMD[dtype, 1], y: SIMD[dtype, 1]):
self.x = x
self.y = y
self.data = SIMD[dtype, 2](self.x, self.y)
self.hash = hash(self.data)

fn __hash__(self) -> Int:
return self.hash

fn __eq__(self, other: Self) -> Bool:
return self.hash == other.hash

fn __ne__(self, other: Self) -> Bool:
return self.hash != other.hash

fn __str__(self) -> String:
return str(self.data)

fn __len__(self) -> Int:
return len(self.data)
alias dtype = DType.int16

@value
struct Position(KeyElement):
var x: SIMD[dtype, 1]
var y: SIMD[dtype, 1]
var data: SIMD[dtype, 2]
var hash: Int

fn __init__(inout self, x: SIMD[dtype, 1], y: SIMD[dtype, 1]):
self.x = x
self.y = y
self.data = SIMD[dtype, 2](self.x, self.y)
self.hash = hash(self.data)

fn __hash__(self) -> Int:
return self.hash

fn __eq__(self, other: Self) -> Bool:
return self.hash == other.hash

fn __ne__(self, other: Self) -> Bool:
return self.hash != other.hash

fn __str__(self) -> String:
return str(self.data)

fn __len__(self) -> Int:
return len(self.data)
19 replies
MModular
Created by swig. on 3/29/2024 in #questions
Tensor Open Source
Hi in the documentation tensors are listed under the standard library category but I can’t find it’s contents in the mojo GitHub repo. Is this not something that is going to be open sourced in the near future?
2 replies
MModular
Created by swig. on 3/9/2024 in #questions
Defining A DynamicVector containing References
I am trying to create a vector that holds a list of object references so that I can manipulate the object's properties from the container but I don't understand how to define the "lifetime" of the Reference type. According to the Mojo docs, it requires three parameters (Reference[?, ?, ?]). I want the lifetime to last for the duration of the parent object's lifetime. Can I get some examples for proper lifetime values when specifying the Reference type parameters?
Reference[Snake, __mlir_attr.`true`, ImmLifetime.DType]
Reference[Snake, __mlir_attr.`true`, ImmLifetime.DType]
5 replies