Skip to content

Commit 892a362

Browse files
authored
Merge pull request #11 from Iainmon/add-readme
Add readme
2 parents 311b2be + b23b2a7 commit 892a362

File tree

3 files changed

+103
-7
lines changed

3 files changed

+103
-7
lines changed

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# ChAI: Chapel Artificial Intelligence
2+
3+
ChAI is a library for AI/ML in [Chapel](https://github.com/chapel-lang/chapel).
4+
Due to Chapel's highly parallel nature, it is well-suited for AI/ML tasks;
5+
the goal of the library is to provide a foundation for such tasks, enabling
6+
local, distributed, and GPU-enabled computations.
7+
8+
Please note that ChAI was developed as part of an internship summer project
9+
by [Iain Moncrief](https://github.com/Iainmon), and as such is in a relatively
10+
early stage of development.
11+
12+
## Overall Design
13+
ChAI intends to provide a PyTorch-like API that is familiar to newcomers from
14+
other languages. To this end, it provides a number of tensor primitives,
15+
including pure-Chapel implementations of operations such as matrix-matrix
16+
multiplication and convolution (in fact, ChAI provides _two_ tensor data types;
17+
see [Static and Dynamic Tensors](#static-and-dynamic-tensors) below). On top
18+
of this low-level API, ChAI defines a layer system, which makes it possible
19+
to compose pieces such as `Conv2D`, `Linear`, and `Flatten` into a feed-forward
20+
neural network.
21+
22+
ChAI's tensors keep track of the computational graph, making it usable for both
23+
feed-forward and back-propagation tasks; however, the feed-forward components
24+
have received more attention at this time.
25+
26+
## Examples
27+
The [examples](https://github.com/chapel-lang/ChAI/tree/main/examples) folder contains
28+
various sample programs written using ChAI.
29+
30+
Thus far, the concrete test for ChAI has been the MNIST dataset; specifically,
31+
ChAI's PyTorch interop has been used to load a pre-trained
32+
convolutional MNIST classifier and execute it on multiple locales.
33+
The [`MultiLocaleInference.chpl`](https://github.com/chapel-lang/ChAI/blob/main/examples/MultiLocaleInference.chpl)
34+
file demonstrates this.
35+
36+
## Getting Started
37+
38+
To use ChAI, you need to have installed Chapel; you can follow the installation
39+
instructions [on this page](https://chapel-lang.org/download.html) to do so.
40+
41+
Once you have Chapel installed, you can use the following command to clone ChAI:
42+
43+
```bash
44+
git clone https://github.com/chapel-lang/ChAI.git
45+
```
46+
47+
You can then compile one of the example ChAI programs using the following
48+
command:
49+
50+
```bash
51+
chpl examples/ConvLayerTest.chpl -M lib
52+
./ConvLayerTest
53+
```
54+
55+
The above should produce the following output:
56+
57+
```
58+
Tensor([ 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0]
59+
[ 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0]
60+
[16.0 17.0 18.0 19.0 20.0 21.0 22.0 23.0]
61+
[24.0 25.0 26.0 27.0 28.0 29.0 30.0 31.0]
62+
[32.0 33.0 34.0 35.0 36.0 37.0 38.0 39.0]
63+
[40.0 41.0 42.0 43.0 44.0 45.0 46.0 47.0]
64+
[48.0 49.0 50.0 51.0 52.0 53.0 54.0 55.0]
65+
[56.0 57.0 58.0 59.0 60.0 61.0 62.0 63.0],
66+
shape = (1, 8, 8),
67+
rank = 3)
68+
69+
Tensor([474.0 510.0 546.0 582.0 618.0 654.0]
70+
[762.0 798.0 834.0 870.0 906.0 942.0]
71+
[1050.0 1086.0 1122.0 1158.0 1194.0 1230.0]
72+
[1338.0 1374.0 1410.0 1446.0 1482.0 1518.0]
73+
[1626.0 1662.0 1698.0 1734.0 1770.0 1806.0]
74+
[1914.0 1950.0 1986.0 2022.0 2058.0 2094.0],
75+
shape = (1, 6, 6),
76+
rank = 3)
77+
```
78+
79+
80+
## Static and Dynamic Tensors
81+
82+
Chapel's type system is static and relatively strict; to iterate over tensors
83+
-- thus implementing various mathematical operations -- the dimensions of
84+
the tensors need to be known at compile-time. However, this does not mesh
85+
well with the ability to dynamically load models from files on disk (since
86+
the contents of the files can be arbitrary).
87+
88+
To mediate between these two requirements, ChAI provides two tensor types:
89+
`StaticTensor` and `DynamicTensor`. The `StaticTensor` includes the rank
90+
of the tensor; this makes it possible to iterate over it and perform the "usual"
91+
operations. The `DynamicTensor` is a rank-erased version of `DynamicTensor`;
92+
it cannot be iterated over, but it can be dynamically cast back to a
93+
`StaticTensor` when needed. Both `StaticTensor` and `DynamicTensor` support
94+
the same operations; `DynamicTensor` performs a dynamic cast to `StaticTensor`
95+
under the hood.

lib/SimpleDomain.chpl

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ proc tuple.eltType type do
4545
// return this(0);
4646

4747
pragma "reference to const when const this"
48-
inline proc tuple.uncons ref : (head.type,tail.type) {
48+
inline proc tuple.uncons ref : (this.head.type,this.tail.type) {
4949
inline proc firstRest(first, rest...) do
5050
return (first,rest);
5151
return firstRest((...this));
@@ -374,7 +374,6 @@ record rect : serializable {
374374
}
375375

376376
proc toString() {
377-
use IO;
378377
use IO.FormattedIO;
379378
const dms = dims();
380379
var content: string = "";
@@ -556,4 +555,4 @@ inline proc isTupleOfOrder(param order: int, tup: ?tupType) param : bool {
556555
foreach idx in rct do
557556
yield indexAt(rct.atIndex(idx));
558557
}
559-
*/
558+
*/

test/loadFromSpec.chpl

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import Time;
66

77
use UnitTest;
88

9+
config const baseDir = "../";
10+
911
proc myTest2(test: borrowed Test) throws {
1012

1113
// Construct the model from specification.
12-
var model: owned Module(real(32)) = Network.loadModel(specFile="scripts/models/cnn/specification.json",
13-
weightsFolder="scripts/models/cnn/",
14+
var model: owned Module(real(32)) = Network.loadModel(specFile=baseDir+"scripts/models/cnn/specification.json",
15+
weightsFolder=baseDir+"scripts/models/cnn/",
1416
dtype=real(32));
1517

1618
// Load an array of images.
1719
const numImages = 10;
18-
var images = forall i in 0..<numImages do Tensor.load("examples/data/datasets/mnist/image_idx_" + i:string + ".chdata") : real(32);
20+
var images = forall i in 0..<numImages do Tensor.load(baseDir+"examples/data/datasets/mnist/image_idx_" + i:string + ".chdata") : real(32);
1921

2022
// Create array of output results.
2123
var preds: [0..<numImages] int;
@@ -29,4 +31,4 @@ proc myTest2(test: borrowed Test) throws {
2931

3032
}
3133

32-
UnitTest.main();
34+
UnitTest.main();

0 commit comments

Comments
 (0)