M
Modular3w ago
Dasor

Confused with conv2d op

Hi everyone, I'm trying to test out the conv2d mojo op in the graph API: https://docs.modular.com/max/api/mojo/graph/ops/convolution/conv2d/ but I'm pretty confused and there are some things I've been trying to figure out for a while now. First of all I don't know what is the RSCF layout and I can't seem to find any information online. This leads me to not really understand how to know which shape the output tensor will have given the shape of and input and a filter. I'm currently running this code ( I will append it in a comment because the message is to large) The code works, but if I change the alias on top for other numbers that in my mind make sense I start getting errors. I've also been looking for example everywhere but I haven't been able to find any. I don't know if I'm making some rookie mistake or If I'm missing some knowledge about convolution so any help is extremely appreciated!. .
1 Reply
Dasor
DasorOP3w ago
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"))
Want results from more Discord servers?
Add your server