จาก ep ที่แล้ว ที่เรากำลังจะช่วยแก้ Bug ใน Tensorboard callback บน GitHub ของ fastai ซึ่งเป็น Bug ที่ทำให้ไม่มีการแสดง Embedding ใน Tensorboard เราได้ศึกษา หาข้อมูล สื่อสาร จนได้ไฟเขียวให้จากทีมงานเรียบร้อย ใน ep นี้เราจะมาเริ่มลงมือแก้ไขโค้ดกันจริง ๆ Commit และ Push ไปไว้บน Github ไปจนสร้าง Pull Request (PR) สำหรับให้ทีมงานเข้ามาดึงโค้ดของเราไป Merge รวมกับโค้ดต้นฉบับ ให้คนทั่วไปได้ใช้ ตามขั้นตอนดังนี้

สมัคร Github (ถ้ายังไม่ได้สมัคร)

ในเคสนี้เราสมัครสมาชิก Github ไว้แล้วในชื่อว่า gnoparus และ install git client ในเครื่องเราแล้วเรียบร้อย

Fork Repository

เนื่องจากเราไม่มีสิทธ์แก้ไข Repository ต้นฉบับที่เก็บ Source Code ของ Fastai ถ้าเราต้องการแก้ไข Source Code วิธีที่สะดวกที่สุด คือ เข้าไปที่ github.com/fastai/fastai แล้วกดปุ่ม Fork ด้านบน เพื่อ Fork Repository ชื่อ fastai/fastai มาเป็น Repository ของเราเอง

Github Fork Button 01
Github Fork Button 01

ในที่นี้คือ gnoparus/fastai ข้างใต้ชื่อจะมีเขียนบอกว่า forked มาจาก fastai/fastai

Forked Repository https://github.com/gnoparus/fastai
Forked Repository https://github.com/gnoparus/fastai

Clone

เมื่อเราได้ Repository fastai ของเราเองมาแล้ว เราจะแก้ไขยังไงก็ได้ ไม่มีผลกระทบต่อ Repository ต้นฉบับ เราจะมา Clone มาที่เครื่องของเราเอง เพื่อแก้ไข Source Code เพื่อแก้ Bug โดยเปิด shell ขึ้นมาแล้วพิมพ์คำสั่ง

git clone https://github.com/gnoparus/fastai
cd fastai
tools/run-after-git-clone

ตอนนี้เราจะมี Folder ขึ้นมาใหม่ชื่อว่า fastai บรรจุ Source Code ทั้งหมดใน Repository พร้อมให้เราแก้ไข

tools/run-after-git-clone เป็นโปรแกรมที่ให้ run หลังจาก clone เพื่อป้องกัน คน commit โค้ดที่ไม่พึงประสงค์ เช่น ตามกฎ fastai จะไม่รับ Jupyter Notebook ที่มี Output ติดมา โปรแกรมนี้จะคอยลบ Output ให้ก่อน Commit

Install editable

ปัญหามีอยู่ว่า ตอนนี้ในเครื่องเรา ก็มี Library fastai อยู่แล้ว ไม่ว่าจะติดมากับ Image หรือที่เรา Install เอง ด้วย pip หรือ conda ทำให้เวลาที่เราเทสโปรแกรม เราจะรู้ได้อย่างไรว่ามันจะไปเรียก Library เวอร์ชันไหน ทางแก้ก็คือ uninstall ออกไปให้หมด แล้วให้ไปเรียกใช้ Library จากที่เราเพิ่ง Clone มาแทน

pip   uninstall fastai
conda uninstall fastai
pip install -e ".[dev]"

pip install -e หมายถึง ให้ install Library แบบ Editable แก้ไขได้ เมื่อเราลองดู sys.path จะเห็นว่ามีการใส่ Folder ที่เรา Clone ไว้ต่อท้ายอยู่

python -c 'import sys, pprint; pprint.pprint(sys.path)'
['',
 '~/.local/lib/python3.8/site-packages',
 '~/anaconda3/envs/fastai/lib/python3.8/site-packages',
 '~/fastai']

Test First

เป็น Practice ที่ดี ถ้าเราจะเขียน Test ไว้ก่อน เราจะได้เทส Before และ After เราแก้ไข ว่า Bug หายไปจริง ๆ ในที่นี้ เราไป Fork และ Clone อีก Repository ไว้ ชื่อ fastai_dev ซึ่งเป็นที่เก็บโปรแกรมสำหรับลองนั่นลองนี่ของ fastai เราสร้างเทสขึ้นมาแบบง่าย ๆ ดังนี้

Test Notebook https://github.com/gnoparus/fastai_dev/blob/master/dev/42_tensorboard_callback_embedding.ipynb
Test Notebook https://github.com/gnoparus/fastai_dev/blob/master/dev/42_tensorboard_callback_embedding.ipynb

เมื่อลอง Run ดู ก็พบว่า Bug ยังอยู่ เพราะว่าเรายังไม่ได้แก้

Edit Source Code

ตอนนี้เราสามารถเปิดไฟล์ Source Code ขึ้นมาแก้ไขได้เลย ซึ่งเราแก้ไข 2 จุด ดังนี้

Fix missing embedding bug https://github.com/gnoparus/fastai/commit/773114b931393b02a1a83b4e378fc3dc14e62bd4
Fix missing embedding bug https://github.com/gnoparus/fastai/commit/773114b931393b02a1a83b4e378fc3dc14e62bd4

เมื่อแก้ไขเรียบร้อยแล้ว เราลองเทสโดยการรัน Test Notebook ที่เราสร้างไว้ ปรากฎว่า Bug หายไป มีการแสดง Embedding ในแท็ป Projector ของ Tensorboard

เมื่อเทสเรียบร้อยแล้ว เราก็จะ Commit และ Push ไปไว้บน Github

git add fastai/callbacks/tensorboard.py
git commit -m “Fix missing embedding”
git push

สร้าง Pull Request

เปิดเว็บไซต์ Github กลับไปที่ Repository ต้นฉบับ ในที่นี้คือ fastai/fastai แล้วกดปุ่ม New pull request

Github New pull request fastai 01
Github New pull request fastai 01

คลิกลิงค์ Compare across forks

Github Create Pull Request Compare Across Forks
Github Create Pull Request Compare Across Forks

เลือก base เป็นต้นฉบับ และ compare กับของเรา

Github Create Pull Request Choose a head repository
Github Create Pull Request Choose a head repository

แล้วกรอกข้อความ title และ description ให้เรียบร้อย อธิบายความเป็นมาของ Bug ตัวนี้ แนบลิงค์ที่เราเคยหารือกับทีมงาน แนบลิงค์โปรแกรมสำหรับเทส

Github Open a pull request Create a new pull request by comparing changes across two branches.
Github Open a pull request Create a new pull request by comparing changes across two branches.

กดปุ่ม Create Pull Request

Github Create Pull Request
Github Create Pull Request

จะได้ Pull Request หน้าตาแบบนี้ สถานะเป็น Open รอให้ทีมงานมาตรวจสอบ

Pull Request Details page with open status
Pull Request Details page with open status

สื่อสาร

เราจะไป Reply กระทู้เดิมที่เราเคยเกริ่นเสนอตัวแก้ Bug ไว้ ว่าตอนนี้เราแก้ไขเสร็จแล้ว สร้าง Pull Request เรียบร้อย สามารถเข้าไปตรวจสอบได้เลย ถ้ามีอะไรต้องแก้ไข ขอให้แจ้งเราได้เลย

Reply in fastai dev forum that pull request has been already submitted
Reply in fastai dev forum that pull request has been already submitted

แก้ไข (ถ้ามี)

ในกรณีที่ต้องมีการแก้ไข ทีมงานจะ Reply แจ้งมาใน Pull Request ให้เราแก้ไขให้เรียบร้อย

สำเร็จแล้ว

เพียงเวลาไม่ถึง 8 ชั่วโมง Pull Request ของเรา สถานะกลายเป็น Closed เรียบร้อย Commit สถานะเป็น Merged แปลว่า โค้ดของเราได้ถูก Merge เข้ากับโค้ดต้นฉบับ ให้คนอื่น ๆ ทั่วโลกได้นำไปใช้ต่อไป

Closed Pull request with merged commit on github fastai
Closed Pull request with merged commit on github fastai

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.