-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMain.lean
More file actions
96 lines (79 loc) · 2.46 KB
/
Main.lean
File metadata and controls
96 lines (79 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/-
Copyright TensorLib Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import Init.System.IO
import Cli
import TensorLib
import TensorLib.Test
open Cli
open TensorLib
def format (p : Parsed) : IO UInt32 := do
let shape : Shape := Shape.mk (p.variableArgsAs! Nat).toList
IO.println s!"Got shape {shape}"
let range := Tensor.arange! Dtype.uint16 shape.count
let v := range.reshape! shape
IO.println v.toNatTree!.format!
return 0
def formatCmd := `[Cli|
"format" VIA format;
"Test formatting"
ARGS:
...shape : Nat; "shape to test"
]
def parseNpy (p : Parsed) : IO UInt32 := do
let file := p.positionalArg! "input" |>.as! String
IO.println s!"Parsing {file}..."
let v <- Npy.parseFile file
IO.println (repr v)
if p.hasFlag "write" then do
let new := (System.FilePath.mk file).addExtension "new"
IO.println s!"Writing copy to {new}"
let _ <- v.save! new
-- TensorLib.Npy.save! (arr : Ndarray) (file : System.FilePath) : IO Unit
return 0
def parseNpyCmd := `[Cli|
"parse-npy" VIA parseNpy;
"Parse a .npy file and pretty print the contents"
FLAGS:
write; "Also write the result back to `input`.new to test saving arrays to disk"
ARGS:
input : String; ".npy file to parse"
]
def runTests (_ : Parsed) : IO UInt32 := do
IO.println "Running Lean tests..."
let t0 <- Test.runAllTests
if !t0 then do
IO.println "Lean tests failed"
return 1
IO.println "Running PyTest..."
let output <- IO.Process.output { cmd := "pytest" }
IO.println s!"stdout: {output.stdout}"
IO.println s!"stderr: {output.stderr}"
return output.exitCode
def runTestsCmd := `[Cli|
"test" VIA runTests;
"Run tests"
]
def tensorlibCmd : Cmd := `[Cli|
tensorlib NOOP; ["0.0.1"]
"TensorLib is a NumPy-like library for Lean."
SUBCOMMANDS:
formatCmd;
parseNpyCmd;
runTestsCmd
]
def main (args : List String) : IO UInt32 :=
if args.isEmpty then do
IO.println tensorlibCmd.help
return 0
else do
tensorlibCmd.validate args