Skip to content

Commit 99ccd26

Browse files
committed
Merge branch 'develop-gp' into develop
2 parents 29cf41b + 8e3dcea commit 99ccd26

8 files changed

Lines changed: 3651 additions & 9 deletions

Elements/pyEEL/notebooks/CG/basicRayTracing.ipynb

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {},
5+
"metadata": {
6+
"slideshow": {
7+
"slide_type": "slide"
8+
}
9+
},
610
"source": [
711
"# This is a `basicRaytracing` python-based example of CPU 3D ray-tracing rendering application\n",
812
"## This notebook has been updated by Prof. George Papagiannakis as an introduction to CPU-based ray-tracing for Elements \n",
@@ -16,17 +20,25 @@
1620
},
1721
{
1822
"cell_type": "markdown",
19-
"metadata": {},
23+
"metadata": {
24+
"slideshow": {
25+
"slide_type": "slide"
26+
}
27+
},
2028
"source": [
21-
"## 1. Overview\n",
29+
"## Overview\n",
2230
"\n",
2331
"The purpose is to create a basic example of ray-tracing, purely on CPU, resulting in creating realistic and engaging images via motivating and easy to follow coding examples. \n",
2432
"\n"
2533
]
2634
},
2735
{
2836
"cell_type": "markdown",
29-
"metadata": {},
37+
"metadata": {
38+
"slideshow": {
39+
"slide_type": "subslide"
40+
}
41+
},
3042
"source": [
3143
"## 1. Output an image in pure Python and PPM format\n",
3244
"The code below outputs a simple background image using the PPM file format based on the original `RayTracingInOneWeekend` book by P. Shirley as found [here](https://github.com/RayTracing/raytracing.github.io/tree/release). \n",
@@ -36,7 +48,11 @@
3648
},
3749
{
3850
"cell_type": "markdown",
39-
"metadata": {},
51+
"metadata": {
52+
"slideshow": {
53+
"slide_type": "subslide"
54+
}
55+
},
4056
"source": [
4157
"Here is an example of the PPM file format, from *Wikipedia*:\n",
4258
"\n",
@@ -45,7 +61,11 @@
4561
},
4662
{
4763
"cell_type": "markdown",
48-
"metadata": {},
64+
"metadata": {
65+
"slideshow": {
66+
"slide_type": "subslide"
67+
}
68+
},
4969
"source": [
5070
"In the above figure, the image grid is `2x3`, thus `y` is the width *(columns)* and `x` is the height *(rows)*. The first line is the header, the second line is the pixel color data. \n",
5171
"\n",
@@ -61,7 +81,11 @@
6181
},
6282
{
6383
"cell_type": "markdown",
64-
"metadata": {},
84+
"metadata": {
85+
"slideshow": {
86+
"slide_type": "subslide"
87+
}
88+
},
6589
"source": [
6690
"# what is row-major order for storage?\n",
6791
"Row-major order is a method of storing or accessing multidimensional arrays in memory. In this method, the elements in each row of the array are stored in consecutive memory locations. This means that when you iterate over the array, you will first go through all the elements in the first row, from left to right, then move on to the second row, and so on.\n",
@@ -2568,7 +2592,8 @@
25682592
"image_width = 1000\n",
25692593
"aspect_ratio = 3/2\n",
25702594
"image_height = int(image_width / aspect_ratio)\n",
2571-
"samples_per_pixel = 3 # For a realistic image you need to set this to 500\n",
2595+
"samples_per_pixel = 3 # For a realistic image you need to set this to 500 (42sec in apple silicon M1max)\n",
2596+
"#samples_per_pixel = 500 # this might take some time to render (3100 sec in apple silicon M1max)\n",
25722597
"max_depth = 50\n",
25732598
"\n",
25742599
"def get_camera():\n",

Elements/pyEEL/notebooks/SciCom/2.8 Python - OpenAI.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
"name": "python",
265265
"nbconvert_exporter": "python",
266266
"pygments_lexer": "ipython3",
267-
"version": "3.8.15"
267+
"version": "3.8.18"
268268
}
269269
},
270270
"nbformat": 4,
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Python: Using other python AI LLM packages - Transformers & pyTorch examples"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### *Copyright 2024-today Dr. George Papagiannakis, papagian@csd.uoc.gr*\n",
15+
"*All Rights Reserved*\n",
16+
"### *University of Crete & Foundation for Research & Technology - Hellas (FORTH)*"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"### Example `Transformers` script"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"You need to install `pip install 'transformers[torch]` to run this script. You also need to install pyTorch as backend for the transformers package: `pip install 'transformers[torch]' `"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"# Load model directly\n",
40+
"from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer\n",
41+
"import torch\n",
42+
"\n",
43+
"# Bug: ValueError: Tokenizer class LlamaTokenizer does not exist or is not currently imported.\n",
44+
"# Solution: pip3 install sentencepiece\n",
45+
"# Info: https://github.com/huggingface/transformers/issues/22222\n",
46+
"\n",
47+
"# Bug: ImportError: cannot import name 'LlamaTokenizer' from 'transformers'\n",
48+
"# Solution: pip3 install git+https://github.com/huggingface/transformers\n",
49+
"# Info: https://stackoverflow.com/questions/75907910/importerror-cannot-import-name-llamatokenizer-from-transformers\n",
50+
"\n",
51+
"tokenizer = AutoTokenizer.from_pretrained(\"mistralai/Mistral-7B-Instruct-v0.2\", padding_side=\"left\")\n",
52+
"model = AutoModelForCausalLM.from_pretrained(\"mistralai/Mistral-7B-Instruct-v0.2\")\n",
53+
"\n",
54+
"while True:\n",
55+
" prompt = input(\"Input your prompt: \")\n",
56+
"\n",
57+
" # https://stackoverflow.com/questions/74748116/huggingface-automodelforcasuallm-decoder-only-architecture-warning-even-after\n",
58+
" input_ids = tokenizer.encode(tokenizer.eos_token + prompt, return_tensors=\"pt\")\n",
59+
" \n",
60+
" print('generating response...')\n",
61+
" output = model.generate(input_ids, max_length=20, pad_token_id=tokenizer.eos_token_id)\n",
62+
"\n",
63+
" decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)\n",
64+
"\n",
65+
" print(\"Response: \", decoded_output)\n"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"### Example `pyTorch` script"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"You need to install `pip install torch` to run this script or for Apple Silicon M1: `conda install pytorch torchvision torchaudio -c pytorch-nightly`"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"# example to verify pyTorch is working\n",
89+
"import torch\n",
90+
"\n",
91+
"if torch.backends.mps.is_available():\n",
92+
" mps_device = torch.device(\"mps\")\n",
93+
" x = torch.ones(1, device=mps_device)\n",
94+
" print (x)\n",
95+
"else:\n",
96+
" print (\"MPS device not found.\")"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"# another example to verify pyTorch is working\n",
106+
"\n",
107+
"import sys\n",
108+
"import platform\n",
109+
"import torch\n",
110+
"import pandas as pd\n",
111+
"import sklearn as sk\n",
112+
"\n",
113+
"has_gpu = torch.cuda.is_available()\n",
114+
"has_mps = getattr(torch,'has_mps',False)\n",
115+
"device = \"mps\" if getattr(torch,'has_mps',False) \\\n",
116+
" else \"gpu\" if torch.cuda.is_available() else \"cpu\"\n",
117+
"\n",
118+
"print(f\"Python Platform: {platform.platform()}\")\n",
119+
"print(f\"PyTorch Version: {torch.__version__}\")\n",
120+
"print()\n",
121+
"print(f\"Python {sys.version}\")\n",
122+
"print(f\"Pandas {pd.__version__}\")\n",
123+
"print(f\"Scikit-Learn {sk.__version__}\")\n",
124+
"print(\"GPU is\", \"available\" if has_gpu else \"NOT AVAILABLE\")\n",
125+
"print(\"MPS (Apple Metal) is\", \"AVAILABLE\" if has_mps else \"NOT AVAILABLE\")\n",
126+
"print(f\"Target device is {device}\")"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"### Example `Tensorflow` script"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"You need to instsall `pip install tensorflow` to run this script. Also for Apple Silicon M1: `pip install tensorflow-macos` and `pip install tensorflow_datasets` as well as `conda install -c apple tensorflow-deps`"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 5,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"# example to verify TensorFlow is working\n",
150+
"\n",
151+
"import tensorflow as tf\n",
152+
"import tensorflow_datasets as tfds"
153+
]
154+
}
155+
],
156+
"metadata": {
157+
"kernelspec": {
158+
"display_name": "elementsProject",
159+
"language": "python",
160+
"name": "python3"
161+
},
162+
"language_info": {
163+
"codemirror_mode": {
164+
"name": "ipython",
165+
"version": 3
166+
},
167+
"file_extension": ".py",
168+
"mimetype": "text/x-python",
169+
"name": "python",
170+
"nbconvert_exporter": "python",
171+
"pygments_lexer": "ipython3",
172+
"version": "3.8.18"
173+
}
174+
},
175+
"nbformat": 4,
176+
"nbformat_minor": 2
177+
}

0 commit comments

Comments
 (0)