pointer to pointer


float** ptrs[] = {
&acts->encoded,

float** ptrs[] = {
&acts->encoded,
acts->encoded is a float* pointer, how to achive this in mojo? I have tried a lot of ways😭
18 Replies
benny
benny5mo ago
are you looking for DTypePointer[DType.float64]?
zhoujing
zhoujing5mo ago
DTypePointer[DType.float64] is a pointer which points to float64, not a pointer I mean a pointer to pointer like float**
benny
benny5mo ago
so you wanta pointer to a pointer to a float? Pointer[DTypePointer[DType.float64]]? im unfamiliar with this syntax
zhoujing
zhoujing5mo ago
yes, I can now get acts->encoded as a pointer in mojo, but i don;t know how to point to the got DtypePointer[Dtype.float32] acts->encoded
ModularBot
ModularBot5mo ago
Congrats @zhoujing, you just advanced to level 1!
zhoujing
zhoujing5mo ago
me too ,haha😆
Martin Dudek
Martin Dudek5mo ago
i stuck also there in my attempt to port llm.c - I assume for &params->wte we would need something like Reference(params[].wte) but didnt yet get around my head how to setup a List of References .. good luck I feel better now, kapai.ai just told me: However, please be aware that the use of Reference is still in its early stages and may be awkward to use. 😉 Pointer.address_of. seems to work, at least in this demo:
alias dtype=DType.float32

fn main():

var sizes = List[Int](3,7,2)

var total = 0
for i in range(len(sizes)):
total += sizes[i]

var mem = DTypePointer[dtype].alloc(total)

var a = DTypePointer[dtype]()
var b = DTypePointer[dtype]()
var c = DTypePointer[dtype]()

var ptrs = List(
Pointer.address_of(a),
Pointer.address_of(b),
Pointer.address_of(c)
)

var pos = 0
for i in range(len(ptrs)):
ptrs[i][] = mem + pos
pos += sizes[i]

b[0] = 17
print(mem[3])
alias dtype=DType.float32

fn main():

var sizes = List[Int](3,7,2)

var total = 0
for i in range(len(sizes)):
total += sizes[i]

var mem = DTypePointer[dtype].alloc(total)

var a = DTypePointer[dtype]()
var b = DTypePointer[dtype]()
var c = DTypePointer[dtype]()

var ptrs = List(
Pointer.address_of(a),
Pointer.address_of(b),
Pointer.address_of(c)
)

var pos = 0
for i in range(len(ptrs)):
ptrs[i][] = mem + pos
pos += sizes[i]

b[0] = 17
print(mem[3])
zhoujing
zhoujing5mo ago
wow, I am porting llm.c too, Thanks for your advice, I will have a try
Martin Dudek
Martin Dudek5mo ago
haven't tried it in the llm.mojo, just getting the individual pieces sorted out right now. Looking forward to see both of our solutions 🙏
ModularBot
ModularBot5mo ago
Congrats @Martin Dudek, you just advanced to level 6!
zhoujing
zhoujing5mo ago
okk,🫰
Martin Dudek
Martin Dudek5mo ago
hope you use regexp search replace in your IDE too, eg for (int (\w+) = 0; \1 < (\w+); \1++) { -> for $1 in range($2): 😉
zhoujing
zhoujing5mo ago
I tried a few hours, still a lot work to do🥺 haha
Martin Dudek
Martin Dudek5mo ago
i stuck with reading in the "gpt2_124M.bin" file - gpt2_build_from_checkpoint did you get this sorted already?
zhoujing
zhoujing5mo ago
I got stuck in this place
typedef struct {
// hyperparameters
int B;
int T;
// input handling and its state
FILE* tokens_file;
long file_size;
long current_position;
// output memory
int* batch;
int* inputs;
int* targets;
// convenience variables
int num_batches;
} DataLoader;
typedef struct {
// hyperparameters
int B;
int T;
// input handling and its state
FILE* tokens_file;
long file_size;
long current_position;
// output memory
int* batch;
int* inputs;
int* targets;
// convenience variables
int num_batches;
} DataLoader;
the FILE pointer I am downloading data now. I will try
Want results from more Discord servers?
Add your server