Dasor
Dasor
MModular
Created by Dasor on 12/6/2024 in #questions
Confused with conv2d op
here is the code:
from random import seed

from max.engine import InferenceSession
from max.graph import Graph, TensorType, ops
from max.tensor import Tensor, TensorShape
from time import perf_counter

alias SIZE = 4096
alias K = 3
alias IN_CH = 3
alias OUT_CH = 3
alias batch = 1
alias GROUPS = 1

def main():
graph = Graph(TensorType(DType.float32, SIZE, SIZE,IN_CH//GROUPS,OUT_CH))
# create a constant tensor value to later create a graph constant symbol
# NHWC format (batch, height, width, channels)
filter_shape = TensorShape(batch, K, K, IN_CH)
constant_value = Tensor[DType.float32](filter_shape).randn(filter_shape) # filter
# create a constant symbol
constant_symbol = graph.constant(constant_value)
# create a convolution node
mm = ops.conv2d(graph[0],constant_symbol,groups=GROUPS)
graph.output(mm)
# verify
graph.verify()

# create session, load and compile the graph
session = InferenceSession()
model = session.load(graph)

# RSCF layout (height, width, in_channels/groups, out_channels)
input_shape = TensorShape(SIZE, SIZE, IN_CH//GROUPS, OUT_CH)
input0 = Tensor[DType.float32](input_shape).randn(input_shape)

# measure time
start = perf_counter()

ret = model.execute("input0", input0^)

print("Inference time:", perf_counter() - start, "seconds")

print("matmul result:", ret.get[DType.float32]("output0"))
from random import seed

from max.engine import InferenceSession
from max.graph import Graph, TensorType, ops
from max.tensor import Tensor, TensorShape
from time import perf_counter

alias SIZE = 4096
alias K = 3
alias IN_CH = 3
alias OUT_CH = 3
alias batch = 1
alias GROUPS = 1

def main():
graph = Graph(TensorType(DType.float32, SIZE, SIZE,IN_CH//GROUPS,OUT_CH))
# create a constant tensor value to later create a graph constant symbol
# NHWC format (batch, height, width, channels)
filter_shape = TensorShape(batch, K, K, IN_CH)
constant_value = Tensor[DType.float32](filter_shape).randn(filter_shape) # filter
# create a constant symbol
constant_symbol = graph.constant(constant_value)
# create a convolution node
mm = ops.conv2d(graph[0],constant_symbol,groups=GROUPS)
graph.output(mm)
# verify
graph.verify()

# create session, load and compile the graph
session = InferenceSession()
model = session.load(graph)

# RSCF layout (height, width, in_channels/groups, out_channels)
input_shape = TensorShape(SIZE, SIZE, IN_CH//GROUPS, OUT_CH)
input0 = Tensor[DType.float32](input_shape).randn(input_shape)

# measure time
start = perf_counter()

ret = model.execute("input0", input0^)

print("Inference time:", perf_counter() - start, "seconds")

print("matmul result:", ret.get[DType.float32]("output0"))
3 replies
MModular
Created by Dasor on 12/1/2024 in #questions
MNIST slower on MAX than on Pytorch
Nevermind I was just missing using the TensorShape as it says on the tensor module example
12 replies
MModular
Created by Dasor on 12/1/2024 in #questions
MNIST slower on MAX than on Pytorch
I'm checking the driver mojo API but I can't even run the code sample provided, do I need the nightly build?
12 replies
MModular
Created by Dasor on 12/1/2024 in #questions
MNIST slower on MAX than on Pytorch
Thanks for the replies I will take a look at those docs. I'm measuring time like this just adding a start variable before the loop and checking after I don't know if that's the right approach, here is a code sample:


start = perf_counter()

for i in range(len(test_dataset)):
item = test_dataset[i]
image = item[0]
label = item[1]

preprocessed_image = preprocess(image)

output = model.execute("input0", preprocessed_image)
probs = output.get[DType.float32]("output0")

predicted = probs.argmax(axis=1)

label_ = Tensor[DType.index](TensorShape(1), int(label))
correct += int(predicted == label_)
total += 1

print("Inference time:", perf_counter() - start, "seconds")


start = perf_counter()

for i in range(len(test_dataset)):
item = test_dataset[i]
image = item[0]
label = item[1]

preprocessed_image = preprocess(image)

output = model.execute("input0", preprocessed_image)
probs = output.get[DType.float32]("output0")

predicted = probs.argmax(axis=1)

label_ = Tensor[DType.index](TensorShape(1), int(label))
correct += int(predicted == label_)
total += 1

print("Inference time:", perf_counter() - start, "seconds")
12 replies