You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

eval_expression.cpp 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "eval_expression.h"
  15. #include <fenv.h>
  16. #include <float.h>
  17. #include <math.h>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <algorithm>
  21. #include <stack>
  22. #include <vector>
  23. #include <string>
  24. namespace pnnx {
  25. static bool token_is_argument(const std::string& t)
  26. {
  27. if (t[0] != '@' || t.size() < 2)
  28. return false;
  29. for (size_t i = 1; i < t.size(); i++)
  30. {
  31. if (t[i] < '0' || t[i] > '9')
  32. return false;
  33. }
  34. return true;
  35. }
  36. static bool token_is_complex(const std::string& t)
  37. {
  38. // 2.000000e+00+3.000000e+00j
  39. if (t[t.size() - 1] != 'j')
  40. return false;
  41. return true;
  42. }
  43. static bool token_is_literal(const std::string& t)
  44. {
  45. if (token_is_complex(t))
  46. return true;
  47. std::istringstream iss(t);
  48. float f;
  49. iss >> std::noskipws >> f;
  50. return iss.eof() && !iss.fail();
  51. }
  52. static bool token_is_interger_literal(const std::string& t)
  53. {
  54. std::istringstream iss(t);
  55. int f;
  56. iss >> std::noskipws >> f;
  57. return iss.eof() && !iss.fail();
  58. }
  59. static std::string eval_expression(const Operator* op)
  60. {
  61. std::string expr = op->params.at("expr").s;
  62. // fprintf(stderr, "eval_expression %s\n", expr.c_str());
  63. // split into tokens
  64. std::vector<std::string> tokens;
  65. {
  66. std::string t;
  67. for (size_t i = 0; i < expr.size(); i++)
  68. {
  69. char ch = expr[i];
  70. if (ch == '[') // list
  71. {
  72. t += ch;
  73. tokens.push_back(t);
  74. t.clear();
  75. }
  76. else if (ch == '(' || ch == ')' || ch == ',' || ch == ']')
  77. {
  78. if (!t.empty())
  79. {
  80. tokens.push_back(t);
  81. t.clear();
  82. }
  83. }
  84. else
  85. {
  86. t += ch;
  87. }
  88. }
  89. if (!t.empty())
  90. {
  91. tokens.push_back(t);
  92. }
  93. }
  94. // scan and stack
  95. std::stack<std::string> exprstack;
  96. for (int i = (int)tokens.size() - 1; i >= 0; i--)
  97. {
  98. const std::string& t = tokens[i];
  99. if (t == "size")
  100. {
  101. std::string a = exprstack.top();
  102. exprstack.pop();
  103. std::string b = exprstack.top();
  104. exprstack.pop();
  105. if (token_is_argument(a) && token_is_literal(b))
  106. {
  107. int input_index = std::stoi(a.substr(1));
  108. if (op->inputs[input_index]->shape.empty())
  109. {
  110. std::string r = std::string("size(") + a + "," + b + ")";
  111. exprstack.push(r);
  112. }
  113. else
  114. {
  115. int bi = std::stoi(b);
  116. if (bi < 0)
  117. bi = op->inputs[input_index]->shape.size() + bi;
  118. int r = op->inputs[input_index]->shape[bi];
  119. if (r == -1)
  120. {
  121. // do not evaluate dynamic size info as -1
  122. // just keep the size expression
  123. std::string r = std::string("size(") + a + "," + b + ")";
  124. exprstack.push(r);
  125. }
  126. else
  127. {
  128. exprstack.push(std::to_string(r));
  129. }
  130. }
  131. }
  132. else
  133. {
  134. std::string r = std::string("size(") + a + "," + b + ")";
  135. exprstack.push(r);
  136. }
  137. }
  138. else if (t == "int"
  139. || t == "abs"
  140. || t == "acos"
  141. || t == "acosh"
  142. || t == "asin"
  143. || t == "asinh"
  144. || t == "atan"
  145. || t == "atanh"
  146. || t == "ceil"
  147. || t == "cos"
  148. || t == "cosh"
  149. || t == "exp"
  150. || t == "floor"
  151. || t == "log"
  152. || t == "log10"
  153. || t == "neg"
  154. || t == "reciprocal"
  155. || t == "round"
  156. || t == "rsqrt"
  157. || t == "sign"
  158. || t == "sin"
  159. || t == "sinh"
  160. || t == "sqrt"
  161. || t == "square"
  162. || t == "tan"
  163. || t == "tanh"
  164. || t == "trunc")
  165. {
  166. std::string a = exprstack.top();
  167. exprstack.pop();
  168. if (token_is_literal(a))
  169. {
  170. float af = std::stof(a);
  171. if (t == "int")
  172. {
  173. int r = int(af);
  174. if (token_is_interger_literal(a))
  175. {
  176. r = std::stoi(a);
  177. }
  178. exprstack.push(std::to_string(r));
  179. }
  180. if (t == "abs")
  181. {
  182. float r = abs(af);
  183. exprstack.push(std::to_string(r));
  184. }
  185. if (t == "acos")
  186. {
  187. float r = acos(af);
  188. exprstack.push(std::to_string(r));
  189. }
  190. if (t == "acosh")
  191. {
  192. float r = acosh(af);
  193. exprstack.push(std::to_string(r));
  194. }
  195. if (t == "asin")
  196. {
  197. float r = asin(af);
  198. exprstack.push(std::to_string(r));
  199. }
  200. if (t == "asinh")
  201. {
  202. float r = asinh(af);
  203. exprstack.push(std::to_string(r));
  204. }
  205. if (t == "atan")
  206. {
  207. float r = atan(af);
  208. exprstack.push(std::to_string(r));
  209. }
  210. if (t == "atanh")
  211. {
  212. float r = atanh(af);
  213. exprstack.push(std::to_string(r));
  214. }
  215. if (t == "ceil")
  216. {
  217. float r = ceil(af);
  218. exprstack.push(std::to_string(r));
  219. }
  220. if (t == "cos")
  221. {
  222. float r = cos(af);
  223. exprstack.push(std::to_string(r));
  224. }
  225. if (t == "cosh")
  226. {
  227. float r = cosh(af);
  228. exprstack.push(std::to_string(r));
  229. }
  230. if (t == "exp")
  231. {
  232. float r = exp(af);
  233. exprstack.push(std::to_string(r));
  234. }
  235. if (t == "floor")
  236. {
  237. float r = floor(af);
  238. exprstack.push(std::to_string(r));
  239. }
  240. if (t == "log")
  241. {
  242. float r = log(af);
  243. exprstack.push(std::to_string(r));
  244. }
  245. if (t == "log10")
  246. {
  247. float r = log10(af);
  248. exprstack.push(std::to_string(r));
  249. }
  250. if (t == "neg")
  251. {
  252. float r = -af;
  253. exprstack.push(std::to_string(r));
  254. }
  255. if (t == "reciprocal")
  256. {
  257. float r = 1.f / af;
  258. exprstack.push(std::to_string(r));
  259. }
  260. if (t == "round")
  261. {
  262. // round to nearest even
  263. int old_rm = fegetround();
  264. fesetround(FE_TONEAREST);
  265. float r = nearbyintf(af);
  266. fesetround(old_rm);
  267. exprstack.push(std::to_string(r));
  268. }
  269. if (t == "rsqrt")
  270. {
  271. float r = 1.f / sqrt(af);
  272. exprstack.push(std::to_string(r));
  273. }
  274. if (t == "sign")
  275. {
  276. float r = af > 0.f ? 1.f : (af == 0.f ? 0.f : -1.f);
  277. exprstack.push(std::to_string(r));
  278. }
  279. if (t == "sin")
  280. {
  281. float r = sin(af);
  282. exprstack.push(std::to_string(r));
  283. }
  284. if (t == "sinh")
  285. {
  286. float r = sinh(af);
  287. exprstack.push(std::to_string(r));
  288. }
  289. if (t == "sqrt")
  290. {
  291. float r = sqrt(af);
  292. exprstack.push(std::to_string(r));
  293. }
  294. if (t == "square")
  295. {
  296. float r = af * af;
  297. exprstack.push(std::to_string(r));
  298. }
  299. if (t == "tan")
  300. {
  301. float r = tan(af);
  302. exprstack.push(std::to_string(r));
  303. }
  304. if (t == "tanh")
  305. {
  306. float r = tanh(af);
  307. exprstack.push(std::to_string(r));
  308. }
  309. if (t == "trunc")
  310. {
  311. float r = trunc(af);
  312. exprstack.push(std::to_string(r));
  313. }
  314. }
  315. else
  316. {
  317. std::string r = t + "(" + a + ")";
  318. exprstack.push(r);
  319. }
  320. }
  321. else if (t == "atan2"
  322. || t == "add"
  323. || t == "sub"
  324. || t == "mul"
  325. || t == "div"
  326. || t == "floor_divide"
  327. || t == "fmod"
  328. || t == "pow"
  329. || t == "remainder")
  330. {
  331. std::string a = exprstack.top();
  332. exprstack.pop();
  333. std::string b = exprstack.top();
  334. exprstack.pop();
  335. if (token_is_literal(a) && token_is_literal(b))
  336. {
  337. float af = std::stof(a);
  338. float bf = std::stof(b);
  339. if (t == "atan2")
  340. {
  341. float r = atan2(af, bf);
  342. exprstack.push(std::to_string(r));
  343. }
  344. if (t == "add")
  345. {
  346. float r = af + bf;
  347. exprstack.push(std::to_string(r));
  348. }
  349. if (t == "sub")
  350. {
  351. float r = af - bf;
  352. exprstack.push(std::to_string(r));
  353. }
  354. if (t == "mul")
  355. {
  356. float r = af * bf;
  357. exprstack.push(std::to_string(r));
  358. }
  359. if (t == "div")
  360. {
  361. float r = af / bf;
  362. exprstack.push(std::to_string(r));
  363. }
  364. if (t == "fmod")
  365. {
  366. float r = fmod(af, bf);
  367. exprstack.push(std::to_string(r));
  368. }
  369. if (t == "floor_divide")
  370. {
  371. int r = (int)af / (int)bf;
  372. exprstack.push(std::to_string(r));
  373. }
  374. if (t == "pow")
  375. {
  376. float r = pow(af, bf);
  377. exprstack.push(std::to_string(r));
  378. }
  379. if (t == "remainder")
  380. {
  381. float r = fmod(af, bf);
  382. if (af * bf < 0)
  383. r += bf;
  384. exprstack.push(std::to_string(r));
  385. }
  386. }
  387. else
  388. {
  389. std::string r = t + "(" + a + "," + b + ")";
  390. exprstack.push(r);
  391. }
  392. }
  393. else if (t == "and" || t == "or" || t == "xor" || t == "lshift" || t == "rshift")
  394. {
  395. std::string a = exprstack.top();
  396. exprstack.pop();
  397. std::string b = exprstack.top();
  398. exprstack.pop();
  399. if (token_is_interger_literal(a) && token_is_interger_literal(b))
  400. {
  401. int ai = std::stoi(a);
  402. int bi = std::stoi(b);
  403. if (t == "and")
  404. {
  405. int r = ai & bi;
  406. exprstack.push(std::to_string(r));
  407. }
  408. if (t == "or")
  409. {
  410. int r = ai | bi;
  411. exprstack.push(std::to_string(r));
  412. }
  413. if (t == "xor")
  414. {
  415. int r = ai ^ bi;
  416. exprstack.push(std::to_string(r));
  417. }
  418. if (t == "lshift")
  419. {
  420. int r = ai << bi;
  421. exprstack.push(std::to_string(r));
  422. }
  423. if (t == "rshift")
  424. {
  425. int r = ai >> bi;
  426. exprstack.push(std::to_string(r));
  427. }
  428. }
  429. else
  430. {
  431. std::string r = t + "(" + a + "," + b + ")";
  432. exprstack.push(r);
  433. }
  434. }
  435. else if (t == "[") // list
  436. {
  437. std::vector<std::string> elements;
  438. while (!exprstack.empty())
  439. {
  440. std::string a = exprstack.top();
  441. exprstack.pop();
  442. elements.push_back(a);
  443. }
  444. std::string r = "[";
  445. for (int j = 0; j < (int)elements.size() - 1; j++)
  446. {
  447. r += elements[j];
  448. if (j + 1 != (int)elements.size())
  449. r += ",";
  450. }
  451. if (!elements.empty())
  452. {
  453. r += elements[elements.size() - 1];
  454. }
  455. r += "]";
  456. exprstack.push(r);
  457. }
  458. else if (t[0] == '@')
  459. {
  460. exprstack.push(t);
  461. }
  462. else
  463. {
  464. // literal
  465. exprstack.push(t);
  466. }
  467. }
  468. std::string r = exprstack.top();
  469. exprstack.pop();
  470. while (!exprstack.empty())
  471. {
  472. r += std::string(",") + exprstack.top();
  473. exprstack.pop();
  474. }
  475. // fprintf(stderr, "eval_expression return %s\n", r.c_str());
  476. return r;
  477. }
  478. static std::string canonicalize_arguments(const Operator* op, std::vector<Operand*>& inputs)
  479. {
  480. std::string expr = op->params.at("expr").s;
  481. // split into tokens
  482. std::vector<std::string> tokens;
  483. {
  484. std::string t;
  485. for (size_t i = 0; i < expr.size(); i++)
  486. {
  487. char ch = expr[i];
  488. if (ch == '[') // list
  489. {
  490. t += ch;
  491. tokens.push_back(t);
  492. t.clear();
  493. }
  494. else if (ch == '(' || ch == ')' || ch == ',' || ch == ']')
  495. {
  496. if (!t.empty())
  497. {
  498. tokens.push_back(t);
  499. t.clear();
  500. }
  501. t += ch;
  502. tokens.push_back(t);
  503. t.clear();
  504. }
  505. else
  506. {
  507. t += ch;
  508. }
  509. }
  510. if (!t.empty())
  511. {
  512. tokens.push_back(t);
  513. }
  514. }
  515. std::string r;
  516. for (auto t : tokens)
  517. {
  518. if (t[0] == '@')
  519. {
  520. int input_index = std::stoi(t.substr(1));
  521. Operand* operand = op->inputs[input_index];
  522. int new_input_index;
  523. auto it = std::find(inputs.begin(), inputs.end(), operand);
  524. if (it == inputs.end())
  525. {
  526. new_input_index = inputs.size();
  527. inputs.push_back(operand);
  528. }
  529. else
  530. {
  531. new_input_index = it - inputs.begin();
  532. }
  533. r += std::string("@") + std::to_string(new_input_index);
  534. }
  535. else
  536. {
  537. r += t;
  538. }
  539. }
  540. // fprintf(stderr, "canonicalize_arguments return %s\n", r.c_str());
  541. return r;
  542. }
  543. void eval_expression(Graph& graph)
  544. {
  545. for (Operator* op : graph.ops)
  546. {
  547. if (op->type != "pnnx.Expression")
  548. continue;
  549. std::string expr_eval = eval_expression(op);
  550. op->params["expr"] = expr_eval;
  551. std::vector<Operand*> inputs;
  552. std::string expr_canonicalize = canonicalize_arguments(op, inputs);
  553. op->params["expr"] = expr_canonicalize;
  554. for (auto r : op->inputs)
  555. {
  556. r->remove_consumer(op);
  557. }
  558. for (auto r : inputs)
  559. {
  560. r->consumers.push_back(op);
  561. }
  562. op->inputs = inputs;
  563. }
  564. }
  565. } // namespace pnnx