Skip to content

New Keras Parser from Uri #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions tmva/sofie/test/KerasParserTest/BatchNormalizationtest.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tensor_batchnormalization1movingvariance0 64
0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394 0.999500394
tensor_batchnormalization1beta0 64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tensor_batchnormalization1gamma0 64
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
tensor_dense17kernel0 448
0.0108048618 0.215931386 0.0123945177 0.285069078 -0.104697779 -0.209598348 0.00423592329 -0.2823928 0.116487086 -0.12513034 0.0747551024 -0.00770813227 0.239529043 0.159453094 -0.28320843 -0.00874188542 -0.157378957 0.0602476597 -0.205106258 0.228819519 0.156571299 -0.234788939 -0.0700670928 -0.263461113 0.143216401 0.0773885548 -0.00458329916 -0.170526713 -0.145017579 0.122938514 -0.290278703 0.0688058138 0.0361943543 -0.0493358076 -0.00392708182 -0.284400582 0.166610718 0.122950584 -0.0912979394 -0.0632623136 -0.19157052 -0.0420778245 -0.0250469148 0.277549595 -0.246620253 0.153357059 -0.193938583 0.0913421512 0.17182675 -0.161206797 -0.146403342 0.168200165 0.0514654517 0.285589248 0.177236408 0.239046842 0.271209925 -0.0295130908 0.243002325 -0.177540243 -0.168115333 -0.228779823 0.0283973515 0.179595679 -0.198399067 0.215017587 0.0389697552 -0.0777019709 -0.0739576519 -0.0860316157 -0.0285128355 -0.142276645 -0.137453258 -0.157670751 0.11001423 -0.251886994 -0.290008962 0.238182813 -0.156130373 -0.248245955 -0.124903694 -0.129840136 0.0469535887 -0.156804115 0.0401303768 0.221745759 -0.0454875827 0.179246753 -0.0117903352 0.0204212666 -0.268830091 0.218235701 -0.264822543 0.151397437 -0.220389888 0.217418164 0.253188759 0.0389984548 0.202692091 0.0823853612 -0.223403066 0.273629099 0.230908662 -0.107497707 -0.110331237 -0.120720729 -0.235759467 0.0507353544 -0.122409225 0.112901539 -0.189501375 -0.118903041 -0.269068241 0.0767331123 -0.209109575 -0.0241218507 0.249974757 0.228964657 0.251895219 -0.0699959695 0.245350212 -0.271214575 -0.226293862 0.245217532 0.0605310798 -0.105240524 -0.181201681 0.276239663 -0.154074326 0.0106938779 0.230460852 0.115790337 0.161239386 0.154280871 0.215945214 0.287186056 -0.226439416 -0.244549721 0.25764963 0.132598132 -0.167660266 -0.124990329 0.0322424471 -0.144228578 -0.0685434043 -0.263268828 0.287399143 -0.167323902 -0.175259233 -0.220007718 0.247749418 0.249331802 0.0275261402 0.285681635 0.224241287 0.279460758 0.0549612343 0.136446476 -0.1061299 -0.211200207 0.190035731 -0.148243546 -0.000311821699 0.231444687 -0.0228230059 -0.199860305 -0.269175649 0.0455857217 -0.259420276 -0.131201625 -0.117612928 -0.189057663 0.185323805 0.0275135934 0.0949172378 -0.0632302165 0.187787294 0.021389991 -0.122828677 -0.231739581 -0.0210861564 0.130167186 0.230645269 -0.204621851 -0.0380035043 0.0184370279 0.0357265174 0.274054497 0.154319197 -0.280433387 0.276268095 0.192360759 0.181914926 -0.10357748 -0.0122078657 0.0994132161 -0.189529791 0.14017576 0.194115937 -0.0822871476 0.190085024 0.28514728 0.196474433 -0.126136974 0.0876276493 0.22874549 -0.0943528563 -0.21709615 -0.166011333 -0.204499945 -0.228483602 -0.029565841 -0.221954465 0.169807643 0.116328508 0.10293144 0.238334745 0.0139791071 -0.279547811 -0.057903856 0.116980433 0.211231321 -0.0950945914 0.0762212574 -0.122185215 -0.2635867 -0.189521894 -0.133030325 0.276025444 -0.274003267 0.0397931337 -0.0262677968 -0.0421609879 -0.205390483 0.265192062 0.169212639 -0.0588397384 -0.113253638 -0.286851257 -0.173200786 0.0343206823 -0.128531307 0.120894819 0.16205284 -0.26693207 -0.103117675 -0.0463741124 0.234895021 0.0869942904 -0.195388302 -0.243550718 0.111745328 0.259278566 0.232909232 0.269730121 0.128117055 -0.285545975 -0.015607506 0.0385112166 0.223638445 0.236599356 0.191709965 -0.19348073 0.067645669 -0.14132981 0.241881877 -0.254734188 -0.228491291 -0.239838064 -0.0670351833 0.154426962 -0.0801543146 0.132534206 -0.157252058 0.139873505 0.103443086 -0.184833378 0.231345206 0.272313625 0.144612879 -0.0427127481 -0.116279498 0.266867608 0.148977041 -0.11838904 -0.243089601 -0.220981985 0.273647815 0.265305191 -0.108035192 -0.00552237034 0.0897967219 0.0963402092 0.257930547 0.00589051843 0.24955228 -0.287052602 -0.216473341 -0.118245646 -0.229325905 0.00889033079 -0.141335711 0.16459924 -0.279934347 0.264600664 0.189448148 0.20955357 0.0990448296 -0.027635932 0.0795616508 0.243308693 0.0665873885 -0.193698078 0.178786546 0.0275886059 0.19980213 0.0518908501 -0.227167904 -0.0288101733 -0.217772186 0.0663120151 0.0809931457 0.23769322 -0.284707844 -0.277737409 0.202413201 -0.0784830749 0.200194508 -0.191186607 -0.163768932 -0.0959100127 -0.24673669 -0.11973758 -0.18587245 0.237517565 0.266499251 -0.0500470549 0.0682872534 -0.114201352 -0.203679815 0.0936342776 -0.0233978629 -0.0219255388 0.190061122 -0.156872734 0.167493641 -0.229817986 -0.132643729 -0.282223672 0.0858635902 -0.12809591 0.14055869 -0.203111768 -0.153313249 -0.0905048549 0.032363534 0.0640204847 -0.11933282 -0.0381491482 0.288048357 0.0544538796 0.0737758577 -0.186934054 -0.0764555931 -0.118738636 0.0113504529 -0.23289682 -0.24103862 0.183179826 -0.0324722826 0.0281182528 -0.106172249 0.137558669 -0.101038292 -0.0941964239 -0.109811425 0.205613256 -0.0358462334 -0.202844858 0.259382457 0.00479933619 0.279637247 -0.250478655 0.201448083 0.0918923318 0.0203225613 0.133025885 -0.160158649 -0.139158651 0.103551477 -0.0765164495 0.235947281 0.0446474254 0.0191914439 -0.115890756 -0.239997402 0.0594975352 -0.206928521 0.283032924 -0.15147914 0.251403838 -0.049104169 -0.129442304 -0.115165785 0.0916467011 -0.0739351213 -0.00163686275 0.0691559017 -0.0499850214 0.13808167 -0.151729494 0.08843413 0.257064074 -0.224859029 -0.0825117826 -0.0263075829 -0.257945806 -0.198194474 0.200157225 -0.120868146 -0.17015183 -0.268784493 0.235713631 0.208676606 -0.0274443626 -0.278948426 -0.220054567 0.093952328 -0.00847533345 0.169271052 -0.109093592 0.111329764 -0.232666582 -0.130706757 0.0706871152 -0.215322942 -0.287200451 -0.0953007936 0.101649582 0.0647162497 -0.266912311 -0.0801778883 -0.205314517 -0.285931677 0.214427441 0.217154175 0.148746103 0.0555344224
tensor_dense17bias0 64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tensor_batchnormalization1movingmean0 64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Binary file not shown.
160 changes: 160 additions & 0 deletions tmva/sofie/test/KerasParserTest/BatchNormalizationtest.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//Code generated automatically by TMVA for Inference of Model file [BatchNormalizationtest.h5] at [Thu Aug 24 08:55:44 202]

#ifndef ROOT_TMVA_SOFIE_BATCHNORMALIZATIONTEST
#define ROOT_TMVA_SOFIE_BATCHNORMALIZATIONTEST

#include<algorithm>
#include<vector>
#include "TMVA/SOFIE_common.hxx"
#include <fstream>

namespace TMVA_SOFIE_BatchNormalizationtest{
namespace BLAS{
extern "C" void saxpy_(const int * n, const float * alpha, const float * x,
const int * incx, float * y, const int * incy);
extern "C" void scopy_(const int *n, const float* x, const int *incx, float* y, const int* incy);
extern "C" void sgemm_(const char * transa, const char * transb, const int * m, const int * n, const int * k,
const float * alpha, const float * A, const int * lda, const float * B, const int * ldb,
const float * beta, float * C, const int * ldc);
extern "C" void sgemv_(const char * trans, const int * m, const int * n, const float * alpha, const float * A,
const int * lda, const float * X, const int * incx, const float * beta, const float * Y, const int * incy);
}//BLAS
struct Session {
std::vector<float> fTensor_batchnormalization1movingvariance0 = std::vector<float>(64);
float * tensor_batchnormalization1movingvariance0 = fTensor_batchnormalization1movingvariance0.data();
std::vector<float> fTensor_batchnormalization1beta0 = std::vector<float>(64);
float * tensor_batchnormalization1beta0 = fTensor_batchnormalization1beta0.data();
std::vector<float> fTensor_batchnormalization1gamma0 = std::vector<float>(64);
float * tensor_batchnormalization1gamma0 = fTensor_batchnormalization1gamma0.data();
std::vector<float> fTensor_dense17kernel0 = std::vector<float>(448);
float * tensor_dense17kernel0 = fTensor_dense17kernel0.data();
std::vector<float> fTensor_dense17bias0 = std::vector<float>(64);
float * tensor_dense17bias0 = fTensor_dense17bias0.data();
std::vector<float> fTensor_batchnormalization1movingmean0 = std::vector<float>(64);
float * tensor_batchnormalization1movingmean0 = fTensor_batchnormalization1movingmean0.data();
std::vector<float> fTensor_batchnormalization1batchnormadd10 = std::vector<float>(64);
float * tensor_batchnormalization1batchnormadd10 = fTensor_batchnormalization1batchnormadd10.data();
std::vector<float> fTensor_dense17BiasAdd0 = std::vector<float>(64);
float * tensor_dense17BiasAdd0 = fTensor_dense17BiasAdd0.data();
std::vector<float> fTensor_dense17bias0bcast = std::vector<float>(64);
float * tensor_dense17bias0bcast = fTensor_dense17bias0bcast.data();


Session(std::string filename ="") {
if (filename.empty()) filename = "BatchNormalizationtest.dat";
std::ifstream f;
f.open(filename);
if (!f.is_open()){
throw std::runtime_error("tmva-sofie failed to open file for input weights");
}
std::string tensor_name;
int length;
f >> tensor_name >> length;
if (tensor_name != "tensor_batchnormalization1movingvariance0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_batchnormalization1movingvariance0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 64) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 64 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_batchnormalization1movingvariance0[i];
f >> tensor_name >> length;
if (tensor_name != "tensor_batchnormalization1beta0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_batchnormalization1beta0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 64) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 64 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_batchnormalization1beta0[i];
f >> tensor_name >> length;
if (tensor_name != "tensor_batchnormalization1gamma0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_batchnormalization1gamma0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 64) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 64 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_batchnormalization1gamma0[i];
f >> tensor_name >> length;
if (tensor_name != "tensor_dense17kernel0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_dense17kernel0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 448) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 448 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_dense17kernel0[i];
f >> tensor_name >> length;
if (tensor_name != "tensor_dense17bias0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_dense17bias0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 64) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 64 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_dense17bias0[i];
f >> tensor_name >> length;
if (tensor_name != "tensor_batchnormalization1movingmean0" ) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor name; expected name is tensor_batchnormalization1movingmean0 , read " + tensor_name;
throw std::runtime_error(err_msg);
}
if (length != 64) {
std::string err_msg = "TMVA-SOFIE failed to read the correct tensor size; expected size is 64 , read " + std::to_string(length) ;
throw std::runtime_error(err_msg);
}
for (int i =0; i < length; ++i)
f >> tensor_batchnormalization1movingmean0[i];
f.close();
{
float * data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_dense17bias0,{ 64 }, { 1 , 64 });
std::copy(data, data + 64, tensor_dense17bias0bcast);
delete [] data;
}
}

std::vector<float> infer(float* tensor_dense17input){

//--------- Gemm
char op_0_transA = 'n';
char op_0_transB = 'n';
int op_0_m = 1;
int op_0_n = 64;
int op_0_k = 7;
float op_0_alpha = 1;
float op_0_beta = 1;
int op_0_lda = 7;
int op_0_ldb = 64;
std::copy(tensor_dense17bias0bcast, tensor_dense17bias0bcast + 64, tensor_dense17BiasAdd0);
BLAS::sgemm_(&op_0_transB, &op_0_transA, &op_0_n, &op_0_m, &op_0_k, &op_0_alpha, tensor_dense17kernel0, &op_0_ldb, tensor_dense17input, &op_0_lda, &op_0_beta, tensor_dense17BiasAdd0, &op_0_n);
constexpr int op_1_N =64;
constexpr int op_1_incx = 1;
constexpr int op_1_incy = 1;
BLAS::scopy_(&op_1_N, tensor_dense17BiasAdd0, &op_1_incx,tensor_batchnormalization1batchnormadd10, &op_1_incy);

float op_1_alpha = -1;
BLAS::saxpy_(&op_1_N, &op_1_alpha, tensor_batchnormalization1movingmean0, &op_1_incx,tensor_batchnormalization1batchnormadd10, &op_1_incy);

for (size_t i = 0; i < 64; i++) {
tensor_batchnormalization1batchnormadd10[i] *= tensor_batchnormalization1gamma0[i] * tensor_batchnormalization1movingvariance0[i];
}
op_1_alpha = 1;
BLAS::saxpy_(&op_1_N, &op_1_alpha, tensor_batchnormalization1beta0, &op_1_incx, tensor_batchnormalization1batchnormadd10, &op_1_incy);

std::vector<float> ret (tensor_batchnormalization1batchnormadd10, tensor_batchnormalization1batchnormadd10 + 64);
return ret;
}
};
} //TMVA_SOFIE_BatchNormalizationtest

#endif // ROOT_TMVA_SOFIE_BATCHNORMALIZATIONTEST
12 changes: 12 additions & 0 deletions tmva/sofie/test/KerasParserTest/CNNtest.dat

Large diffs are not rendered by default.

Binary file added tmva/sofie/test/KerasParserTest/CNNtest.h5
Binary file not shown.
Loading