Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions tmva/sofie/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,14 @@ if (ROOT_TORCH_FOUND)
configure_file(RecurrentModelGenerator.py RecurrentModelGenerator.py COPYONLY)

if (BLAS_FOUND)
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c"
"import torch; print(getattr(torch, '__version__', 'unknown'))"
RESULT_VARIABLE status
OUTPUT_VARIABLE module_version
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
if(${status} EQUAL 0 AND ${module_version} VERSION_LESS 2.9.0)
# Disable TestSofieModels which is failing on alma8, alma10, ubuntu2204, and ubuntu2404
# with PyTorch 2.9.0
ROOT_ADD_GTEST(TestSofieModels TestSofieModels.cxx
LIBRARIES
ROOTTMVASofie
ROOTTMVASofieParser
BLAS::BLAS
INCLUDE_DIRS
${CMAKE_CURRENT_BINARY_DIR}
)
endif()
ROOT_ADD_GTEST(TestSofieModels TestSofieModels.cxx
LIBRARIES
ROOTTMVASofie
ROOTTMVASofieParser
BLAS::BLAS
INCLUDE_DIRS
${CMAKE_CURRENT_BINARY_DIR}
)
endif()
endif()

Expand Down
48 changes: 25 additions & 23 deletions tmva/sofie/test/Conv1dModelGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

### generate COnv2d model using Pytorch

import numpy as np

Check failure on line 5 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

tmva/sofie/test/Conv1dModelGenerator.py:5:17: F401 `numpy` imported but unused
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F

Check failure on line 9 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

tmva/sofie/test/Conv1dModelGenerator.py:5:1: I001 Import block is un-sorted or un-formatted


result = []


class Net(nn.Module):

def __init__(self, nc = 1, ng = 1, nl = 4, use_bn = False, use_maxpool = False, use_avgpool = False):
super(Net, self).__init__()

Expand All @@ -23,11 +23,11 @@
self.use_bn = use_bn
self.use_maxpool = use_maxpool
self.use_avgpool = use_avgpool

self.conv0 = nn.Conv1d(in_channels=self.nc, out_channels=4, kernel_size=2, groups=1, stride=1, padding=1)
if (self.use_bn): self.bn1 = nn.BatchNorm2d(4)

Check failure on line 28 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:28:25: E701 Multiple statements on one line (colon)
if (self.use_maxpool): self.pool1 = nn.MaxPool2d(2)

Check failure on line 29 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:29:30: E701 Multiple statements on one line (colon)
if (self.use_avgpool): self.pool1 = nn.AvgPool2d(2)

Check failure on line 30 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:30:30: E701 Multiple statements on one line (colon)
if (self.nl > 1):
# output is 4x4 with optionally using group convolution
self.conv1 = nn.Conv1d(in_channels=4, out_channels=8, groups = self.ng, kernel_size=3, stride=1, padding=1)
Expand All @@ -42,7 +42,7 @@
x = self.bn1(x)
if (self.use_maxpool or self.use_avgpool):
x = self.pool1(x)
if (self.nl == 1) : return x

Check failure on line 45 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:45:25: E701 Multiple statements on one line (colon)
x = self.conv1(x)
x = F.relu(x)

Expand All @@ -55,7 +55,7 @@
parser = argparse.ArgumentParser(description='PyTorch model generator')
parser.add_argument('params', type=int, nargs='+',
help='parameters for the Conv network : batchSize , inputChannels, inputImageSize, nGroups, nLayers ')

parser.add_argument('--bn', action='store_true', default=False,
help='For using batch norm layer')
parser.add_argument('--maxpool', action='store_true', default=False,
Expand All @@ -69,13 +69,13 @@


args = parser.parse_args()

#args.params = (4,2,4,1,4)

np = len(args.params)

Check failure on line 75 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F811)

tmva/sofie/test/Conv1dModelGenerator.py:75:4: F811 Redefinition of unused `np` from line 5
if (np < 5) : exit()

Check failure on line 76 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:76:16: E701 Multiple statements on one line (colon)
bsize = args.params[0]
nc = args.params[1]
nc = args.params[1]
d = args.params[2]
ngroups = args.params[3]
nlayers = args.params[4]
Expand All @@ -85,31 +85,31 @@
# use_1d = args.oneD

print ("using batch-size =",bsize,"nchannels =",nc,"dim =",d,"ngroups =",ngroups,"nlayers =",nlayers)
if (use_bn): print("using batch normalization layer")

Check failure on line 88 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:88:15: E701 Multiple statements on one line (colon)
if (use_maxpool): print("using maxpool layer")

Check failure on line 89 in tmva/sofie/test/Conv1dModelGenerator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

tmva/sofie/test/Conv1dModelGenerator.py:89:20: E701 Multiple statements on one line (colon)

#sample = torch.zeros([2,1,5,5])
input = torch.zeros([])
for ib in range(0,bsize):
xa = torch.ones([1, 1, d]) * (ib+1)
if (nc > 1) :
if (nc > 1) :
xb = xa.neg()
xc = torch.cat((xa,xb),1) # concatenate tensors
if (nc > 2) :
xd = torch.zeros([1,nc-2,d])
xc = torch.cat((xa,xb,xd),1)
else:
xc = xa
#concatenate tensors
if (ib == 0) :

#concatenate tensors
if (ib == 0) :
xinput = xc
else :
xinput = torch.cat((xinput,xc),0)
xinput = torch.cat((xinput,xc),0)

print("input data",xinput.shape)
print(xinput)

name = "Conv1dModel"
if (use_bn): name += "_BN"
if (use_maxpool): name += "_MAXP"
Expand All @@ -120,24 +120,26 @@
loadModel=False
savePtModel = False


model = Net(nc,ngroups,nlayers, use_bn, use_maxpool, use_avgpool)
print(model)

model(xinput)

model.forward(xinput)

if savePtModel :
torch.save({'model_state_dict':model.state_dict()}, name + ".pt")

if saveOnnx:
torch.onnx.export(
model,
xinput,
name + ".onnx",
export_params=True
)
torch.onnx.export(
model,
xinput,
name + ".onnx",
export_params=True,
dynamo=True,
external_data=False
)

if loadModel :
print('Loading model from file....')
Expand All @@ -158,10 +160,10 @@

f = open(name + ".out", "w")
for i in range(0,outSize):
f.write(str(float(yvec[i]))+" ")
f.write(str(float(yvec[i].detach()))+" ")




if __name__ == '__main__':
main()
57 changes: 33 additions & 24 deletions tmva/sofie/test/Conv2dModelGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
result = []

class Net(nn.Module):

def __init__(self, nc = 1, ng = 1, nl = 4, use_bn = False, use_maxpool = False, use_avgpool = False):
super(Net, self).__init__()

Expand All @@ -22,7 +22,7 @@ def __init__(self, nc = 1, ng = 1, nl = 4, use_bn = False, use_maxpool = False,
self.use_bn = use_bn
self.use_maxpool = use_maxpool
self.use_avgpool = use_avgpool

self.conv0 = nn.Conv2d(in_channels=self.nc, out_channels=4, kernel_size=2, groups=1, stride=1, padding=1)
if (self.use_bn): self.bn1 = nn.BatchNorm2d(4)
if (self.use_maxpool): self.pool1 = nn.MaxPool2d(2)
Expand All @@ -32,7 +32,7 @@ def __init__(self, nc = 1, ng = 1, nl = 4, use_bn = False, use_maxpool = False,
self.conv1 = nn.Conv2d(in_channels=4, out_channels=8, groups = self.ng, kernel_size=3, stride=1, padding=1)
#output is same 4x4
self.conv2 = nn.Conv2d(in_channels=8, out_channels=4, kernel_size=3, stride=1, padding=1)
#use stride last layer
#use stride last layer
self.conv3 = nn.Conv2d(in_channels=4, out_channels=1, kernel_size=2, stride=2, padding=0)


Expand Down Expand Up @@ -60,7 +60,7 @@ def main():
parser = argparse.ArgumentParser(description='PyTorch model generator')
parser.add_argument('params', type=int, nargs='+',
help='parameters for the Conv network : batchSize , inputChannels, inputImageSize, nGroups, nLayers ')

parser.add_argument('--bn', action='store_true', default=False,
help='For using batch norm layer')
parser.add_argument('--maxpool', action='store_true', default=False,
Expand All @@ -72,13 +72,13 @@ def main():


args = parser.parse_args()

#args.params = (4,2,4,1,4)

np = len(args.params)
if (np < 5) : exit()
bsize = args.params[0]
nc = args.params[1]
nc = args.params[1]
d = args.params[2]
ngroups = args.params[3]
nlayers = args.params[4]
Expand All @@ -94,24 +94,24 @@ def main():
input = torch.zeros([])
for ib in range(0,bsize):
xa = torch.ones([1, 1, d, d]) * (ib+1)
if (nc > 1) :
if (nc > 1) :
xb = xa.neg()
xc = torch.cat((xa,xb),1) # concatenate tensors
if (nc > 2) :
xd = torch.zeros([1,nc-2,d,d])
xc = torch.cat((xa,xb,xd),1)
else:
xc = xa
#concatenate tensors
if (ib == 0) :

#concatenate tensors
if (ib == 0) :
xinput = xc
else :
xinput = torch.cat((xinput,xc),0)
xinput = torch.cat((xinput,xc),0)

print("input data",xinput.shape)
print(xinput)

name = "Conv2dModel"
if (use_bn): name += "_BN"
if (use_maxpool): name += "_MAXP"
Expand All @@ -122,24 +122,33 @@ def main():
loadModel=False
savePtModel = False


model = Net(nc,ngroups,nlayers, use_bn, use_maxpool, use_avgpool)
print(model)

model(xinput)

model.forward(xinput)

if savePtModel :
torch.save({'model_state_dict':model.state_dict()}, name + ".pt")


if saveOnnx:
torch.onnx.export(
model,
xinput,
name + ".onnx",
export_params=True
)

#new ONNX exporter does not work for batchmorm
dynamo_export=True
if (use_bn): dynamo_export=False

torch.onnx.export(
model,
xinput,
name + ".onnx",
export_params=True,
dynamo=dynamo_export,
external_data=False
)


if loadModel :
print('Loading model from file....')
Expand All @@ -160,10 +169,10 @@ def main():

f = open(name + ".out", "w")
for i in range(0,outSize):
f.write(str(float(yvec[i]))+" ")
f.write(str(float(yvec[i].detach()))+" ")




if __name__ == '__main__':
main()
Loading
Loading